package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;

    @Override
    public PageResponse<DeviceDataVo> getDeviceDataPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(),deviceDataPageReqDto.getPageSize());
        Long startTime = deviceDataPageReqDto.getStartTime();
        Long endTime = deviceDataPageReqDto.getEndTime();
        deviceDataPageReqDto.setStartDate(ObjectUtil.isEmpty(startTime)? null : LocalDateTimeUtil.of(startTime));
        deviceDataPageReqDto.setEndDate(ObjectUtil.isEmpty(endTime)? null : LocalDateTimeUtil.of(endTime));
        Page<DeviceDataVo> deviceDataVos = deviceDataMapper.getDeviceDataPage(deviceDataPageReqDto);
        PageResponse<DeviceDataVo> response = PageResponse.of(deviceDataVos, DeviceDataVo.class);
        return response;
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        //1.将long类型时间戳转换为LocalDateTime
        LocalDateTime start = LocalDateTimeUtil.of(startTime);
        log.info("==================== {}",start);
        LocalDateTime end = LocalDateTimeUtil.of(endTime);

        //2.调用mapper查询数据库已有的每个小时统计物模型平均数据值,返回List<DeviceDataGraphVo> list1
        List<DeviceDataGraphVo> list1 = deviceDataMapper.queryDeviceDataListByDay(iotId,functionId,start,end);
        //3.构建24个小时事件列表List<DeviceDataGraphVo>("00:00","01:00",...,"23:00") list2
        List<DeviceDataGraphVo> list2 = DeviceDataGraphVo.dayInstance(start);

        //4.遍历list2每个元素，将每个元素去list1匹配将相同时间数据值赋值给list2中相同时间的数据值, 不匹配的小时赋值0
            for (DeviceDataGraphVo deviceDataGraphVo : list2) {
                for (DeviceDataGraphVo dataGraphVo : list1) {
                    if (deviceDataGraphVo.getDateTime().equals(dataGraphVo.getDateTime())) {
                        deviceDataGraphVo.setDataValue(dataGraphVo.getDataValue());
                    }
                }
            }
        //5.返回数据list2
        return list2;
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        //将long 类型时间戳转换为localDataTime
        LocalDateTime start = LocalDateTimeUtil.of(startTime);
        LocalDateTime end = LocalDateTimeUtil.of(endTime);
        //2,调用mapper 查询数据库已有的每个小时统计物模型平均数据值，返回List<DeviceDateGrophVo> list1
        List<DeviceDataGraphVo> list1 = deviceDataMapper.queryDeviceDataListByWeek(iotId,functionId,start,end);

        List<DeviceDataGraphVo> list2 = DeviceDataGraphVo.weekInstance(start);

        for (DeviceDataGraphVo deviceDataGraphVo : list2) {
            for (DeviceDataGraphVo dataGraphVo : list1) {
                if (deviceDataGraphVo.getDateTime().equals(dataGraphVo.getDateTime())){
                    deviceDataGraphVo.setDataValue(dataGraphVo.getDataValue());
                }
            }
        }
        return list2;
    }
}
